<template>
    <div class="dict-type">
        <el-card class="!border-none" shadow="never">
            <div>
                <el-button
                    type="primary"
                    @click="handleAdd"
                >
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
                <el-button
                    v-perms="['banner/delete']"
                    :disabled="!selectData.length"
                    type="danger"
                    @click="handleDelete(selectData)"
                >
                    <template #icon>
                        <icon name="el-icon-Delete" />
                    </template>
                    删除
                </el-button>
            </div>
            <div class="mt-4" v-loading="pager.loading">
                <div>
                    <el-table
                        :data="pager.lists"
                        size="large"
                    >
                        <el-table-column label="标题" prop="title"  />
                        <el-table-column label="图片">
                            <template #default="{ row }">
                                <image-contain
                                    v-if="row.url"
                                    :src="row.url"
                                    :width="60"
                                    :height="45"
                                    :preview-src-list="[row.url]"
                                    preview-teleported
                                    fit="contain"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column label="位置">
                            <template v-slot="{ row }">
                                <el-tag v-if="row.type == 1">技术特色</el-tag>
                                <el-tag v-else>首页</el-tag>
                            </template>
                        </el-table-column> 
                        
                        <el-table-column label="操作" width="190" fixed="right">
                            <template #default="{ row }">
                                <el-button
                                    link
                                    type="primary"
                                    @click="handleEdit(row)"
                                >
                                    编辑
                                </el-button>
                                <el-button
                                    link
                                    type="danger"
                                    @click="handleDelete(row.id)"
                                >
                                    删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <div class="flex justify-end mt-4">
                    <pagination v-model="pager" @change="getLists" />
                </div>
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
  </template>
  
  <script setup name="banner">
  import { getBannerList,deletBanner } from '@/apis/banner'
  import { usePaging } from '@/hooks/usePaging' 
  import feedback from '@/utils/feedback'
  import EditPopup from './edit.vue'
  const editRef = shallowRef()
  const showEdit = ref(false)
  const queryParams = reactive({
    type: 0,
})

  
  const { pager, getLists } = usePaging({
    fetchFun: getBannerList, 
    params:queryParams
  })
  
  const selectData = ref([])
  
  const handleSelectionChange = (val) => {
    selectData.value = val.map(({ id }) => id)
  }
  
  const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
  }
  
  const handleEdit = async (data) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
  }
  
  // 删除角色
  const handleDelete = async (id) => {
    await feedback.confirm('确定要删除？')
    await deletBanner({ id })
    getLists()
  }
  
  onMounted(() => {
  getLists()
  })
  </script>
  